package top.llove.controller;

import cn.hutool.setting.Setting;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.llove.service.ImageService;
import top.llove.utils.FileUtils;
import top.llove.utils.GitUtils;
import top.llove.utils.res.ApiResult;

import java.io.IOException;

/**
 * 图床Controller
 *
 * @author: 幸会
 * @date: 2021/6/8 20:05
 * @email: 1846724873@qq.com
 * @description: 图床Controller
 * version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/img")
public class ImageController {

    @Autowired
    private ImageService img;

    /**
     * 文件上传
     * @param file 文件
     * @return
     * @throws IOException
     * @throws GitAPIException
     */
    @PostMapping("/upload")
    public ApiResult upload(@RequestParam("img") MultipartFile file) throws IOException, GitAPIException {
        return img.upload(file);
    }


    /**
     * 克隆仓库
     * @return
     */
    @SneakyThrows
    @PostMapping("/clone")
    public ApiResult clone() {
        try {
            GitUtils.cloneGit();
            FileUtils.mkdir(new Setting("config.setting").get("xh.img.upload"));
            return ApiResult.success("克隆成功", null);
        } catch (GitAPIException e) {
            e.printStackTrace();
            return ApiResult.fail("克隆失败", null);
        }
    }
}
